package com.tianci.appstore.ui.mainpage;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.coocaa.x.app.framework.app.LiteAppApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.skyworth.framework.skysdk.ipc.SkyStartActivity;
import com.tianci.appstore.AppDetailActivity;
import com.tianci.appstore.GameZoneActivity;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.control.SkyMainPageController;
import com.tianci.appstore.ui.data.SkyAppBaseData;
import com.tianci.appstore.ui.data.SkyAppDetailData;
import com.tianci.appstore.ui.data.SkyMainAppData;
import com.tianci.appstore.ui.util.UICommon;
import com.tianci.appstore.ui.widget.AlwaysMarqueeTextView;
import com.tianci.appstore.ui.widget.CatchImageView;
import com.tianci.appstore.ui.widget.SkyBaseItemView;
import com.tianci.appstore.ui.widget.TipToast;
import com.tianci.appstore.utils.SkyConstants;
import com.tianci.appstore.utils.SkyNetImageCache;

import java.util.HashMap;
import java.util.Map;

public class MainBigBlock extends SkyBaseItemView {
    private ImageView posterImage;
    private static int titleSize = SkyConstants.Dpi(42);
    protected TextView installedText;
    protected static final int installedTextSize = SkyConstants.Dpi(24);

    protected SkyAppDetailData.AppStatus status = SkyAppDetailData.AppStatus.NOT_DOWNLOADED;

    public MainBigBlock(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void createView() {
        // TODO Auto-generated method stub
        posterImage = new CatchImageView(mContext);
        posterImage.setScaleType(ScaleType.FIT_XY);
        //SkinManager.setBackground(posterImage, R.drawable.bg);
        //posterImage.setImageDrawable(SkinManager.getResDrawable(R.drawable.bg));
        this.addView(posterImage, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        titleText = new AlwaysMarqueeTextView(mContext);
//        titleText.setGravity(Gravity.CENTER);
//        titleText.setSelected(false);
//        titleText.setTextSize(titleSize);
//        int titleBgColor = SkinManager.getColor(R.color.titleCoverColor);
//        titleText.setBackgroundColor(titleBgColor);
//        titleText.setPadding(SkyConstants.Div(65), 0, SkyConstants.Div(65), 0);
//        SkinManager.setTextColor(titleText, R.color.titleColor);
//        LayoutParams titleParams = new LayoutParams(LayoutParams.MATCH_PARENT, SkyConstants.Div(106), Gravity.BOTTOM);
//        this.addView(titleText, titleParams);
    }

    @Override
    public void update(SkyAppBaseData data) {
        // TODO Auto-generated method stub
        if (this.data != null && this.data.getIconUrl() != null && data != null && this.data.getIconUrl().equals(data.getIconUrl())) {
            return;
        }
        this.data = data;
        LogUtils.i("detail", "Updating, data = " + data.toString());

        UICommon.setText(titleText, data.getTitle(), true);
        String posterUrl = ((SkyMainAppData) data).getPosterUrl();
        posterImage.setImageResource(R.drawable.pic_nopic);
        if (posterUrl != null) {
            LogUtils.d("lqw", "update---" + data.getId());
            SkyNetImageCache.getSkyNetImageCache().downloadUrl("", posterUrl, this, SkyConstants.Div(512), SkyConstants.Div(512), 10);
        }
        posterUrl = null;
    }

    private void refreshStatus() {
        if (status == SkyAppDetailData.AppStatus.INSTALLED || status == SkyAppDetailData.AppStatus.INSTALLED_WITH_CAN_NOT_BE_UNINSTALLED) {
            if (installedText == null) {
                installedText = UICommon.newTextView(mContext, installedTextSize, R.color.defaultColor);
            }
            installedText.setBackgroundResource(R.drawable.poster_installed_bg);
            installedText.setText(R.string.installed);
            installedText.setWidth(SkyConstants.Div(113));
            installedText.setHeight(SkyConstants.Div(48));
            installedText.setGravity(Gravity.CENTER);
            if (installedText.getParent() == null) {
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(SkyConstants.Div(113), SkyConstants.Div(48), Gravity.BOTTOM | Gravity.RIGHT);
                addView(installedText, params);
            }
        } else {
            if (installedText != null && installedText.getParent() != null) {
                removeView(installedText);
            }
        }
    }

    public void updateStatus(SkyAppDetailData.AppStatus status) {
        this.status = status;
        refreshStatus();
    }

    @Override
    protected void refreshImage(String url, Bitmap result) {
        // TODO Auto-generated method stub
        String logStr = "-------------------- refreshing poster image";
        if (result == null || url != ((SkyMainAppData) data).getPosterUrl()) {
            LogUtils.d("image", logStr + ", result == null || url doesn't match !!!!!!!!!!");
            return;
        }
        LogUtils.i("image", logStr + ", set bitmap success");
        posterImage.setImageBitmap(result);
    }

    @Override
    public void onClick(View v) {
        LogUtils.i("detail", "bigBlock, onClick, data = " + data.toString());
        LogUtils.i("log","the index is " + mIndex);
        if (!LiteAppApplication.checkNetworkConnection(TipToast.showNetIsNotConnectedToastRunnable))
            return;

        String strId = this.data.getId();
        if (strId == null || strId.trim().length() == 0)
            return;
        String type = ((SkyMainAppData) data).getType();
        Intent intent;
        if (type != null && type.contains("1")) {
            intent = new Intent(mContext, GameZoneActivity.class);
            intent.putExtra("id", this.data.getId());
//            mContext.startActivity(intent);
        } else {
            intent = new Intent(mContext, AppDetailActivity.class);
            intent.putExtra("id", this.data.getId());
//            mContext.startActivity(intent);
        }
        HashMap<String,String> map = new HashMap<String,String>();
        SkyStartActivity.startActivity( mContext, mContext.getClass().getName(), intent, map, true);
        Map<String,String> params = new HashMap<String, String>();
        params.put("category", SkyMainPageController.getCurrentCategoryData().name + "页");
        params.put("id",String.valueOf(mIndex -2));
        params.put("block_content_name",data.getTitle());
        params.put("block_content_id",data.getPackageName());
        LogUtils.submit(LogUtils.PRODUCTID_APPSTORE, "as2_main_block_onclick", params);
        super.onClick(v);
    }

    @Override
    public void destory() {
        // TODO Auto-generated method stub
        LogUtils.d("destory", "mainbigblock");
        if (posterImage != null)
            posterImage.setImageBitmap(null);
    }
}
